# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.jsonUtils_v2.table2Json_Chen import Table2Json as table_chen
from com.zzh.utils.table2Json_v2 import Table2Json as table
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField


class DepartmentDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertDept(self, info):
        """
        添加 department 信息
        :param info:
        :return:
        """
        deptName = info['deptName']
        deptPcode = info['deptPcode']
        deptCode = info['deptCode']
        deptIsLock = info['deptIsLock']
        deptSort = info['deptSort']
        deptAddress = info['deptAddress']
        leveCode = info['leveCode']
        provinceId = info['provinceId']
        cityId = info['cityId']
        districtId = info['districtId']
        deptUrl = info['deptUrl']
        desUrl = info['desUrl']
        extractFrom = info['extractFrom']

        sql = "insert into zzh_department (dept_name, dept_pcode, dept_code,dept_is_lock,dept_sort," \
              "dept_address,dept_url,des_url,extract_from,leve_code,province_id,city_id,district_id) VALUES " \
              "(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [deptName, deptPcode, deptCode, deptIsLock, deptSort, deptAddress, deptUrl, desUrl,
                               extractFrom, leveCode, provinceId,
                               cityId, districtId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findDeptById(self, deptId):
        """
        根据ID查询 department 信息
        :param deptId:
        :return:
        """
        sql = "select * from zzh_department WHERE dept_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [deptId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = table.dept_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findDeptByCondition(self, **kwargs):
        """
        department 表的条件查询
        :param kwargs:
        :return:
        """

        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        fieldMap = requestFieldMapToDBField.viewDeptFieldMap
        whereSentence = SqlUtils.getOption(conditions,fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_dept WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_dept WHERE " + whereSentence
        rs = cur.execute(sql)

        jsons = None
        if rs:
            rsSet = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex, pageSize, rsSet)
            jsons = table_chen.view_dept_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons, rs

    def deleteDept(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_department WHERE dept_id=%s"
        cur = self.conn.cursor()
        rs = 0
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateDept(self, info):
        """
        更新 department 表
        :param info:
        :return:
        """
        deptId = info['deptId']
        deptName = info['deptName']
        deptPcode = info['deptPcode']
        deptCode = info['deptCode']
        deptIsLock = info['deptIsLock']
        deptSort = info['deptSort']
        deptAddress = info['deptAddress']
        leveCode = info['leveCode']
        provinceId = info['provinceId']
        cityId = info['cityId']
        districtId = info['districtId']
        deptUrl = info['deptUrl']
        desUrl = info['desUrl']
        extractFrom = info['extractFrom']

        sql = "update zzh_department set dept_name=%s,dept_pcode=%s,dept_code=%s,dept_is_lock=%s,dept_sort=%s," \
              "dept_address=%s,leve_code=%s,province_id=%s,city_id=%s,district_id=%s,dept_url=%s,des_url=%s,extract_from=%s " \
              " WHERE dept_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [deptName, deptPcode, deptCode, deptIsLock, deptSort, deptAddress, leveCode, provinceId,
                               cityId, districtId, deptUrl, desUrl, extractFrom, deptId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectDept(self, deptId):
        """
        根据ID查询，返回元组
        :param deptId:
        :return: data tuple
        """
        sql = "select * from zzh_department WHERE dept_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [deptId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr
